package com.wnhoo.smartfishtank.api;

import android.util.Log;

import com.google.gson.Gson;
import com.wnhoo.smartfishtank.api.data.DataTiming;
import com.wnhoo.smartfishtank.api.data.LM_DataReceive;
import com.wnhoo.smartfishtank.api.data.LM_DataSend;
import com.wnhoo.smartfishtank.util.MyLog;
import com.wnhoo.smartfishtank.util.UtilsHex;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import java.io.IOException;
import java.util.List;

/**
 * Created by kevinwen on 2014/12/27.
 * Copyright (c) 2014/12/27.KevinWen. All rights reserved.
 */
public class LM_TimingAPI {

    private final static String TAG = "LM_TimingAPI";
    private final static String sensor_type = "1";
    private final static String SENSOR_ID = "S90";
    private static final Gson gson;

    static {

        gson = new Gson();
    }

    /**
     * @param deviceId
     * @param token_value
     * @return List<DataTiming> or NULL
     * @throws ClientProtocolException
     * @throws IOException
     * @throws JSONException
     */
    public static LM_DataReceive requestTimingList(String deviceId, String token_value) throws ClientProtocolException, IOException, JSONException

    {
        MyLog.d(TAG, "requestTimingList");
        String result = LM_BasicAPI.requestReceive(true, deviceId, SENSOR_ID, sensor_type, token_value);
        Log.d(TAG, "requestTimingList"+result);
        LM_DataReceive dataReceive = gson.fromJson(result, LM_DataReceive.class);
        return dataReceive;
    }

    /**@param timingSwitch 定时功能的总开关1，表示当前定时功能开启，0表示当前定时功能关闭,2表示当前是默认数据
     * @param list
     * @return DataSend or null
     */
    public static LM_DataSend sendTimingList(List<DataTiming> list, String deviceId, String token_value,int timingSwitch ) throws ClientProtocolException, IOException {
        String data = null;
        int size = 0;
        //将List<DataTiming> list转为协议中的数据controlDataTemp
        //判断当前的列表中有哪些是设置了定时的
        for (DataTiming dataTiming : list) {
            if (dataTiming.isSend_timing()) {
                size++;
            }
        }
//        int size = list.size();
        StringBuffer sb = new StringBuffer();
        sb.append(timingSwitch).append("(");
        for (int i = 0; i < size; i++) {
            DataTiming dataTiming = list.get(i);
            // Chimo
            Integer colorNumber = Integer.valueOf(dataTiming.getColor(), 16);
            String colorNumberStr = "";
            if(colorNumber >= 10 && colorNumber <=15) {
                colorNumberStr = "0"+ Integer.toHexString(colorNumber);
//                colorNumberStr = dataTiming.getColor();
            } else {
                colorNumberStr = dataTiming.getColor();
            }
            Log.i(TAG, "dataTiming.getColor ---->" + Integer.valueOf(dataTiming.getColor(), 16));
            Log.i(TAG, "timing_list_data details ---->" + colorNumberStr + ", size :" + i);
            sb.append(colorNumberStr);
            if (i + 1 < size) sb.append(",");
        }
        sb.append(";)");
        data = sb.toString();
        Log.i(TAG, "timing_list_data" + data.toString());
        Log.i(TAG, "deviceId----->" + deviceId);
        String result = LM_BasicAPI.requestSend(true, deviceId, SENSOR_ID, sensor_type, data, token_value);
        return gson.fromJson(result, LM_DataSend.class);
        //return BasicAPI.requestSend(true, deviceId, SENSOR_ID, sensor_type, controlDataTemp, token_value);
    }
}
